package com.eascs.banknet.boc.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.banknet.boc.vo.bocb02.BOCB02Element;
import com.eascs.banknet.common.dao.BaseO2oDao;
import com.eascs.banknet.common.entity.CreditDetailEntity;
import com.eascs.banknet.common.entity.CreditEntity;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;

@Repository
public class BOCB02Dao extends BaseO2oDao{
	public List<BOCB02Element> queryData(String startId){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t2.bank_applyno 'APPNO', user_surname 'CCTPERTRN_PERLSTNM',")
			.append("user_name 'CCTPERTRN_PERFSTNM',document_type 'CCTPERTRN_CBICERTY',document_no 'CCTPERTRN_CBICERNO'")
			.append(" FROM o2o_loanapply t1,o2o_loan_user t2 ")
			.append(" WHERE t1.loanapply_no = t2.loanapply_no ")
			.append("   AND t1.islock = 'N' AND t2.bank_status IS NOT NULL and (t1.data_source is null or t1.data_source!='2') ")
			.append("   AND t1.bank_type = 'BC' ")
			.append("   AND t1.wf_type <> 'NEW' ")
			.append("   AND t2.bank_status = '01B3' ")
			.append("   AND NOT EXISTS(SELECT 1 FROM o2o_credit t3 WHERE t1.loanapply_no=t3.loanapply_no)")
			.append("   AND  t2.bank_applyno IS NOT NULL");
		if(StringUtil.hasText(startId)){
			sql.append("   AND t2.bank_applyno > '").append(startId).append("' ");
		};
		sql.append(" ORDER BY t2.bank_applyno LIMIT 1");
		return super.findEntitys(sql.toString(), BOCB02Element.class);
	}
	
	@Transactional("o2oTransactionManager")
	public void saveInfo(String loanapplyNo, org.dom4j.Element output){
		try {
			//获取xml主数据
			String appno = output.elementText("APPNO");
			String creditLimitAmt =output.elementText("CREDITLIMITAMT");//信用卡授信总量
			String creditOverlimit6m = output.elementText("CREDITLIMITAMT");//信用卡最近6个月平均透支额度
			String creditLimitRate = output.elementText("CREDITLIMITRATE");//信用卡透支额度占比
			CreditEntity creditEntity = new CreditEntity();
			creditEntity.setLoanapplyNo(loanapplyNo);
			creditEntity.setCreditLimitAmt(new BigDecimal(creditLimitAmt));
			creditEntity.setAvgCardusedAmt(new BigDecimal(creditOverlimit6m));
			creditEntity.setCreditLimitRate(new BigDecimal(creditLimitRate));
			creditEntity.setCreator("system");
			creditEntity.setCreateTime(new Date());
			int rw = super.insert(creditEntity);
			if(rw <= 0){
				throw new RuntimeException("保存授信信息失败！");
			}
			List<org.dom4j.Element> outputList = output.elements("LOANDETAIL");//信贷交易信息明细
			if (!outputList.isEmpty()){
				for (org.dom4j.Element element : outputList) {
					CreditDetailEntity detailEntity = new CreditDetailEntity();
					String seqNo = element.elementText("SEQNO");//序号
					String loanType = element.elementText("LOANTYPE");//贷款种类
					String status = element.elementText("STATUS");//账户状态
					String loanAmt = element.elementText("LOANAMT");//贷款金额
					String remainAmt = element.elementText("REMAINAMT");//本金余额
					String loanEndDate = element.elementText("LOANENDDT");//贷款到期日
					String repaymentOfMonth = element.elementText("REPAYAMTOFMONTH");//本月应还款
					String orgType = element.elementText("ORGTYPE");//机构类型
					String guarStyle = element.elementText("GUARSTYLE");//担保方式
					String fiveClass = element.elementText("FIVECLASS");//五级分类
					detailEntity.setCreditId(creditEntity.getId());
					detailEntity.setSeqNo(seqNo);
					detailEntity.setLoanType(loanType);
					detailEntity.setStatus(status);
					detailEntity.setLoanAmt(new BigDecimal(loanAmt));
					detailEntity.setRemainAmt(new BigDecimal(remainAmt));
					String dateStr = loanEndDate.substring(0,4)+"-"+loanEndDate.substring(4, 6)+"-"+loanEndDate.substring(6,8);
					detailEntity.setLoanEndDate(DateUtils.formateStandDate(dateStr));
					detailEntity.setRepayAmtOfMonth(new BigDecimal(repaymentOfMonth));
					detailEntity.setOrgType(orgType);
					detailEntity.setGuarStyle(guarStyle);
					detailEntity.setFiveClass(fiveClass);
					 //将中行接口返回的数据反写
					int rw1 = super.insert(detailEntity);
					if(rw1 <= 0){
						throw new RuntimeException("保存授信信息详情失败！");
					}
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("保存授信信息失败......");
		}

	}
}
